package com.noxplay.noxplayer

import android.app.Application
import android.content.res.Configuration
import android.util.Log
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactNativeApplicationEntryPoint.loadReactNative
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.common.ReleaseLevel
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint
import com.facebook.react.defaults.DefaultReactNativeHost
import expo.modules.ReactNativeHostWrapper
import com.otahotupdate.OtaHotUpdate
import com.nativenoxmodule.NativeNoxModulePackage
import com.nativenoxmodule.NativeWidgetModulePackage
import expo.modules.ApplicationLifecycleDispatcher

class MainApplication : Application(), ReactApplication {
    override val reactNativeHost: ReactNativeHost =
        ReactNativeHostWrapper(this, object : DefaultReactNativeHost(this) {


            override fun getPackages(): List<ReactPackage> =
                PackageList(this).packages.apply {
                    // Packages that cannot be autolinked yet can be added manually here, for example:
                    // add(MyReactNativePackage())
                    add(NativeNoxModulePackage())
                    add(NativeWidgetModulePackage())
                }

            override fun getJSMainModuleName(): String = "index"
            override fun getJSBundleFile(): String {
                val sharedPref = this@MainApplication.getSharedPreferences(
                    "com.noxplay.noxplayer.APMSettings", MODE_PRIVATE)
                if (sharedPref.getBoolean("safemode", false)) {
                    Log.d("APM", "launching in safe mode")
                    with (sharedPref.edit()) {
                        putBoolean("safemode", false)
                        apply()
                    }
                    return "assets://index.android.bundle"
                }
                return OtaHotUpdate.bundleJS(this@MainApplication)
            }
            override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
            override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
            override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
        })


    override val reactHost: ReactHost
        get() = ReactNativeHostWrapper.createReactHost(applicationContext, reactNativeHost)

    override fun onCreate() {
        super.onCreate()
        try {
//          DefaultNewArchitectureEntryPoint.releaseLevel = ReleaseLevel.valueOf(BuildConfig.REACT_NATIVE_RELEASE_LEVEL.uppercase())
        } catch (e: IllegalArgumentException) {
          DefaultNewArchitectureEntryPoint.releaseLevel = ReleaseLevel.STABLE
        }
        DefaultNewArchitectureEntryPoint.releaseLevel = ReleaseLevel.STABLE

        loadReactNative(this)
        ApplicationLifecycleDispatcher.onApplicationCreate(this)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig)
    }
}
